<template>
  <div class="home">
    <home-header ></home-header>
    <home-swiper :swiperList="swiperList"></home-swiper>
    <home-icons :iconList="iconList"></home-icons>
    <home-recommend :recommendList="recommendList"></home-recommend>
    <home-weekend :weekendList="weekendList"></home-weekend>
  </div>
</template>

<script>
import HomeHeader from "./components/HomeHeader";
import HomeSwiper from "./components/HomeSwiper";
import HomeIcons from "./components/HomeIcons";
import HomeRecommend from "./components/HomeRecommend";
import HomeWeekend from "./components/HomeWeekend";
import axios from 'axios'
import {mapState} from 'vuex'

export default {
  name: "Home",
  components: {
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeRecommend,
    HomeWeekend
  },
  data(){
    return {
      lastCity:'',
      swiperList:[],
      iconList:[],
      recommendList:[],
      weekendList:[]
    }
  },
  computed:{
    ...mapState(['city'])
  },
  methods:{
    getHomeInfo(){
      axios.get('/api/index?city='+this.city).then(this.getHomeInfoSuc)
    }, 
    getHomeInfoSuc(res){
      res=res.data
      if(res.ret && res.data){
        this.swiperList=res.data.swiperList
        this.iconList=res.data.iconList
        this.recommendList=res.data.recommendList
        this.weekendList=res.data.weekendList
      }
    }
  },
  mounted(){
    this.lastCity=this.city
    this.getHomeInfo()
  }, 
  activated(){
    if(this.lastCity!== this.city){
      this.lastCity=this.city
      this.getHomeInfo()
    }
  }
};
</script>
